package com.tavio.vaultboy;

import org.andengine.engine.camera.BoundCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.tmx.util.exception.TMXLoadException;
import org.andengine.input.sensor.acceleration.AccelerationData;
import org.andengine.input.sensor.acceleration.IAccelerationListener;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import com.tavio.vaultboy.element.VaultBoy;
import com.tavio.vaultboy.element.state.FallingState;
import com.tavio.vaultboy.element.state.GravityState;
import com.tavio.vaultboy.game.GameUpdater;
import com.tavio.vaultboy.scene.SceneManager;
import com.tavio.vaultboy.world.WorldMap;

public class VaultBoyActivity extends SimpleBaseGameActivity implements IAccelerationListener, IOnSceneTouchListener{
	public static final int CAMERA_WIDTH = 800;
	public static final int CAMERA_HEIGHT = 480;
	
	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TiledTextureRegion mVaultBoyTextureRegion;
	
	private BitmapTextureAtlas boxTextureAtlas;
	private ITextureRegion boxTextureRegion;
	
	private VaultBoy vaultBoy;
	
	private GravityState state;
	private BitmapTextureAtlas movementControllerTextureAtlas;
	private ITextureRegion movementControllerTextureRegion;
	private BitmapTextureAtlas jumpControllerTextureAtlas;
	private ITextureRegion jumpControllerTextureRegion;
	
	private BoundCamera camera;	
	
	public EngineOptions onCreateEngineOptions() {
		camera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera); 
		
		engineOptions.getTouchOptions().setNeedsMultiTouch(true);
		
		return engineOptions;
	}
	
	@Override
	protected void onCreateResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 128, 128);
		this.boxTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 204, 171);
		this.movementControllerTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 168, 64);
		this.jumpControllerTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 60, 60);
		this.mVaultBoyTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this.getAssets(), "soldier.png", 0, 0, 3, 1);
		this.boxTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.boxTextureAtlas, this, "box.png", 0, 0);
		this.movementControllerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.movementControllerTextureAtlas, this, "controller.png", 0, 0);
		this.jumpControllerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.jumpControllerTextureAtlas, this, "button.png", 0, 0);
		this.boxTextureAtlas.load();
		this.movementControllerTextureAtlas.load();
		this.jumpControllerTextureAtlas.load();
		this.mBitmapTextureAtlas.load();
	}
	
	@Override
	public Scene onCreateScene() {
		try {
			this.mEngine.registerUpdateHandler(new FPSLogger());
	
			vaultBoy = new VaultBoy(0, 0, 45, 60, 15f, this.mVaultBoyTextureRegion, this.getVertexBufferObjectManager());
			vaultBoy.setFallingState(FallingState.FALLING);
			
			SceneManager.getInstance().init(camera, this.getVertexBufferObjectManager(), movementControllerTextureRegion, jumpControllerTextureRegion, boxTextureRegion, CAMERA_WIDTH, CAMERA_HEIGHT, 168, 64, vaultBoy);
			WorldMap.getInstance().init("tmx/world.tmx", this.getAssets(), this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, this.getVertexBufferObjectManager());
			
			SceneManager.getInstance().collidableElements.add(vaultBoy);
			
			SceneManager.getInstance().getScene().setBackground(new Background(0, 0, 0));
			SceneManager.getInstance().getScene().attachChild(WorldMap.getInstance().getTmxLayer(0));
			
			camera.setBounds(0, 0, WorldMap.getInstance().getWorldWidth(), WorldMap.getInstance().getWorldHeight());
			camera.setBoundsEnabled(true);
			camera.setChaseEntity(vaultBoy.getSprite());
			
			WorldMap.getInstance().getTmxLayer(0).setZIndex(0);
			SceneManager.getInstance().getScene().attachChild(vaultBoy.getSprite());
			
			state = GravityState.DOWN;
			
			SceneManager.getInstance().getScene().sortChildren();
			
			this.mEngine.registerUpdateHandler(new GameUpdater(0.03f, vaultBoy));
			
			vaultBoy.x = WorldMap.getInstance().getStartPositionX();
			vaultBoy.y = WorldMap.getInstance().getStartPositionY();
			vaultBoy.getSprite().setPosition(vaultBoy.x, vaultBoy.y);
			vaultBoy.getBoundingBox().update();
			
			return SceneManager.getInstance().getScene();
		
		} catch (TMXLoadException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) {

	}

	public void onAccelerationChanged(final AccelerationData pAccelerationData) {
		SceneManager.getInstance().changeGravity(pAccelerationData);
	}
	
	@Override
	public void onResumeGame() {
		super.onResumeGame();

		this.enableAccelerationSensor(this);
	}

	@Override
	public void onPauseGame() {
		super.onPauseGame();

		this.disableAccelerationSensor();
	}

	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
		
		return false;
	}
}